﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class EditHotelCost
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'If Request.Cookies("Admin") Is Nothing Then
            If Session.Item("UserID") Is Nothing Then
                'Response.Redirect("Login.aspx")
            End If
            If Not IsPostBack = True Then
                'Fill_ComboBox(cmbHotel, "SELECT [Id],[Name] FROM [Hotels] where [IsActive]=1 order by Name")
                Fill_ComboBox(cmbHotel, "SELECT [Id],([Country] + ' - ' + [Region] + ' - ' + [City] + ' - ' + [Name]) as Name FROM [ptn_Hotels] where [IsActive]=1 order by Country, Region, City, Name")
                additem(cmbHotel, "--Select Hotel--", 0)
                Fill_ComboBox(cmbRoomCat, "SELECT [Id],[Name] from [ptn_RoomCategory] where [IsActive]=1 order by Name")
                additem(cmbRoomCat, "--Select--", 0)
                Fill_ComboBox(cmbRoom, "SELECT [Id],[Name] from [ptn_RoomType] where [IsActive]=1 order by Id")
                additem(cmbRoom, "--Select--", 0)

                Fill_ComboBox(cmbMeal, "select Id, Name from ptn_MealType order by Name")

                'Dim txt As Array
                'txt = RetriveData("Select Name ")
                'btnSave.Attributes.Add("onclick", "return outofindexdates();")
                If Not Request.QueryString("id") Is Nothing Then
                    fillData(Request.QueryString("id"))
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        arr = RetriveData("SELECT [Id],[StartDate],[EndDate],[HotelId],[RoomTypeId], MealType,[Cost],[IsActive], RoomCategoryId,[ClientType] FROM [ptn_HotelCost] where Id=" & Id, 9)
        txtStartDate.Text = DisplayDate(arr(1))
        txtEndDate.Text = DisplayDate(arr(2))
        cmbHotel.SelectedValue = arr(3)
        cmbRoom.SelectedValue = arr(4)
        cmbMeal.SelectedValue = arr(5)
        txtCost.Text = formatPrice2(arr(6))
        chkActive.Checked = arr(7)
        cmbRoomCat.SelectedValue = arr(8)
        cmbClient.SelectedValue = arr(9)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            'Session.LCID = 2057
            Dim strActive As String
            If chkActive.Checked = True Then
                strActive = 1
            Else
                strActive = 0
            End If
            Dim sDate As DateTime
            Dim eDate As DateTime
            If Request.QueryString("Id") Is Nothing Then
                sDate = txtStartDate.Text 'DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", Nothing)
                eDate = txtEndDate.Text 'DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", Nothing)

                Dim txt As Array
                txt = RetriveData("select Id  from ptn_HotelCost where [StartDate]='" & sDate & "' and [EndDate]='" & eDate & "' and [HotelId]=" & cmbHotel.SelectedValue & " and [RoomCategoryId]=" & cmbRoomCat.SelectedValue & " and [RoomTypeId]=" & cmbRoom.SelectedValue & " and [MealType]=" & cmbMeal.SelectedValue & " and [ClientType]='" & cmbClient.SelectedValue & "'", 0)
                If Not txt(0) Is Nothing Then
                    lblMsg.Text = "Cost is already exist for these parameters."
                Else
                    Insert_Update_Delete_Record("INSERT INTO [ptn_HotelCost]([StartDate],[EndDate],[HotelId],[RoomCategoryId],[RoomTypeId],[Cost],[IsActive],[MealType],[ClientType],[CreatedOn],[UpdatedOn]) VALUES('" & sDate & "','" & eDate & "'," & cmbHotel.SelectedValue & "," & cmbRoomCat.SelectedValue & "," & cmbRoom.SelectedValue & ",'" & txtCost.Text & "'," & strActive & ",'" & cmbMeal.SelectedValue & "','" & cmbClient.SelectedValue & "','" & Date.Now & "','" & Date.Now & "')")
                    Response.Redirect("ViewHotelCost.aspx")
                End If
            Else
                'Session.LCID = 2057
                sDate = txtStartDate.Text 'DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", Nothing)
                eDate = txtEndDate.Text 'DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", Nothing)
                Response.Write("Update [ptn_HotelCost] set [StartDate]='" & sDate & "',[EndDate]='" & eDate & "',[HotelId]=" & cmbHotel.SelectedValue & ",[RoomCategoryId]=" & cmbRoomCat.SelectedValue & ",[RoomTypeId]=" & cmbRoom.SelectedValue & ",[Cost]='" & txtCost.Text & "',[IsActive]=" & strActive & ",[MealType]='" & cmbMeal.SelectedValue & "',[ClientType]='" & cmbClient.SelectedValue & "',[UpdatedOn]='" & Date.Now & "' where id=" & Request.QueryString("Id"))
                'Response.Redirect("ViewHotelCost.aspx")
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("ViewHotelCost.aspx")
    End Sub
End Class